﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BasicClass;
using Microsoft.Win32;
using Common;
using DealFile;

namespace DealPLC
{
    /// <summary>
    /// WinCheckRegLocalLocal.xaml 的交互逻辑
    /// </summary>
    public partial class WinCheckRegLocal : BaseWindow
    {
        #region 窗体单实例
        private static WinCheckRegLocal g_WinCheckRegLocal = null;
        public static WinCheckRegLocal GetWinInst(out bool blNew)
        {
            blNew = false;
            try
            {
                if (g_WinCheckRegLocal == null)
                {
                    blNew = true;
                    g_WinCheckRegLocal = new WinCheckRegLocal();
                }
                return g_WinCheckRegLocal;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError("WinCheckRegLocal", ex);
                return null;
            }
        }

        public static WinCheckRegLocal GetWinInst()
        {
            try
            {
                return g_WinCheckRegLocal;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError("WinCheckRegLocal", ex);
                return null;
            }
        }
        #endregion 窗体单实例

        #region 定义
        public List<TimeLog> Time_L = new List<TimeLog>();
        List<int[]> Value_L = new List<int[]>();
        CheckReg C_I = new CheckReg();

        List<PLCWriteLog> PLCWriteLog_L = new List<PLCWriteLog>();
        #endregion 定义

        #region 初始化
        public WinCheckRegLocal()
        {
            InitializeComponent();
        }
        #endregion 初始化

        #region 读日志
        ///打开本地PLC读取日志
        private void btnLocal_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.FileName = "";
                openFileDialog.Filter = "PLCLog (*.Log)|*.Log";
                openFileDialog.InitialDirectory = ComValue.c_PathPLCLog;

                if (!(bool)openFileDialog.ShowDialog())
                {
                    return;
                }

                if (openFileDialog.FileName.Trim() == "")
                {
                    MessageBox.Show("文件名不能为空！");
                    return;
                }

                string path = openFileDialog.FileName;
                lblPath.Content = path;

                TxtFile file = new TxtFile();
                string txt = file.ReadText(path);
                string[] str = txt.Split('\n');

                Time_L.Clear();
                Value_L.Clear();
                int num = 0;
                for (int i = 0; i < str.Length - 1; i++)
                {
                    if (str[i].Contains("*"))
                    {
                        string time = str[i].Replace("*", "");
                        Time_L.Add(new TimeLog()
                        {
                            No= num,
                            Time = time,
                        });//日志的时间
                        num++;

                        string[] strValue = str[i + 2].Split(' ');
                        List<int> valuePLC_L = new List<int>();
                        for (int j = 0; j < strValue.Length; j++)
                        {
                            if (strValue[j].Trim() != "")
                            {
                                valuePLC_L.Add(int.Parse(strValue[j].Trim()));
                            }
                        }

                        C_I.AddValue_WithoutHeart(valuePLC_L.ToArray());//加入列表，本地记录的读取日志，不包含心跳数据
                        Value_L.Add(valuePLC_L.ToArray());//当前文件所有的PLC值
                    }
                }
                ShowPar_Invoke();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }


        ///切换时间
        private void dgTime_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (dgTime.IsMouseOver)
                {
                    base.IndexP = dgTime.SelectedIndex;
                    for (int i = IndexP - 9; i < IndexP + 1; i++)
                    {
                        if (i > -1)
                        {
                            C_I.AddValue(Value_L[i]);//加入列表
                        }
                    }
                    RefreshReg();
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
        #endregion 读日志

        #region 写日志
        private void btnLocalWrite_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.FileName = "";
                openFileDialog.Filter = "PLCLog (*.Log)|*.Log";
                openFileDialog.InitialDirectory = ComValue.c_PathPLCLog;

                if (!(bool)openFileDialog.ShowDialog())
                {
                    return;
                }

                if (openFileDialog.FileName.Trim() == "")
                {
                    MessageBox.Show("文件名不能为空！");
                    return;
                }

                string path = openFileDialog.FileName;
                lblPathWrite.Content = path;

                TxtFile file = new TxtFile();
                string txt = file.ReadText(path);
                string[] str = txt.Split('\n');

                PLCWriteLog_L.Clear();
                int num = 0;
                for (int i = 0; i < str.Length - 1; i++)
                {
                    if (str[i].Contains("*"))
                    {
                        string time = str[i].Replace("*", "");

                        PLCWriteLog_L.Add(new PLCWriteLog()
                        {
                            No = num,
                            Time = time,
                            Function = str[i + 1].Replace('\r', ' '),
                            Reg = str[i + 2].Replace('\r', ' ').Trim(),
                            Value = str[i + 3].Replace('\r', ' '),
                            TimeWrite = str[i + 4].Replace('\r', ' ').Replace("Time:", ""),
                        });

                        num++;
                    }
                }
                RefreshWrite();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
        #endregion 写日志


        #region 显示
        public override void ShowPar()
        {
            try
            {
                RefreshReg();
                RefreshTime();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        void RefreshReg()
        {
            try
            {
                dgReg.ItemsSource = C_I.CheckReg_L;
                dgReg.Items.Refresh();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        void RefreshTime()
        {
            try
            {
                dgTime.ItemsSource = Time_L;
                dgTime.Items.Refresh();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
        void RefreshWrite()
        {
            try
            {
                dgRegWrite.ItemsSource = PLCWriteLog_L;
                dgRegWrite.Items.Refresh();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
        #endregion 显示

        #region 关闭
        private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
            e.Cancel = true;
        }

        #endregion 关闭

    }

    public class TimeLog : BaseClass
    {
        public string Time { set; get; }
    }

    public class PLCWriteLog : BaseClass
    {
        public string Time { set; get; }//日志时间
        public string Reg { set; get; }//寄存器
        public string Value { set; get; }//数值
        public string Function { set; get; }//写入函数
        public string TimeWrite { set; get; }//写入时间
    }
}
